#define DTP 1000 
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <libgen.h>
#include "sharedseg.h"
#include "nPatriciaTrie.h"
#include "linkedlist.h"
#include "tagdefs.h"
#include "server.h"
#include "fileperm.h"
#include "stopwatch.h"

int main(int argc, char *argv[]) {
	if (argc>=2) {
		#undef INDEX_FILE
		strcpy(customindex,argv[1]);
		printf("Using custom index file : %s\n",customindex);
	}
	
	readfromfile();
	char *st;

	linkedlist *lst;
	/*addtag("nandakiran","tag3");
	addtag("nandakiran","tag1");
	addtag("vivekanand","tag1");
	addtag("vivekanand","tag2");
	addtag("nandagopal","tag2");
	
	lst=listfiles("tag1");
	if (lst!=NULL) {
		st=lst->toString();
		printf("tag1  : %s \n",st);
		free(st);delete lst;
	}
	lst=listfiles("tag2");
	if (lst!=NULL) {
		st=lst->toString();
		printf("tag2  : %s \n",st);
		free(st);delete lst;
	}
	lst=listfiles("tag3");
	if (lst!=NULL) {
		st=lst->toString();
		printf("tag3  : %s \n",st);
		free(st);delete lst;
	}
	
	
	removefile("nandakiran");
	removetag("vivekanand","tag1");
	addtag("nandakiran","tag1");
	lst=listfiles("tag1");
	if (lst!=NULL) {
		st=lst->toString();
		printf("tag1  : %s \n",st);
		free(st);delete lst;
	}
	lst=listfiles("tag2");
	if (lst!=NULL) {
		st=lst->toString();
		printf("tag2  : %s \n",st);
		free(st);delete lst;
	}
	lst=listfiles("tag3");
	if (lst!=NULL) {
		st=lst->toString();
		printf("tag3  : %s \n",st);
		free(st);delete lst;
	}
*/
	printf("Loaded\n");
	printf("File : %ld \nTags : %ld\n",File->triesize,Tags->triesize);







// --------------------
// Destroy Trie's
// --------------------
	nPatriciaTrieNode<nPatriciaTrie<char>*> *t;
	lst=File->getList();
	while ((lst!=NULL) && (!lst->isEmpty())) {
		st=lst->remove();
		t = File->LookupNode(st);
		delete t->GetData();
		t->SetData(NULL);
		free(st);
	}
	delete lst;
	lst=Tags->getList();
	while ((lst!=NULL) && (!lst->isEmpty())) {
		st=lst->remove();
		t = Tags->LookupNode(st);
		delete t->GetData();
		t->SetData(NULL);
		free(st);
	}
	delete lst;
	File->deleteMemory();
	Tags->deleteMemory();
	delete File;
	delete Tags;
	
	free (commonheadkey);
}
